import 'dart:io';

import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:flutterlib/common/config/config.dart';
import 'package:flutterlib/common/utils/sp_utils.dart';
import 'package:flutterlib/common/utils/utils.dart';

import 'interceptors/log_interceptor.dart';


///dio 用于file 上传下载
class HttpManagerFile {
  static Dio _dio;

  static Map<String, CancelToken> _cancelTokens =
      new Map<String, CancelToken>();

  HttpManagerFile._internal() {
    initDio();
  }

  Dio getDio(){
    return _dio;
  }

  static HttpManagerFile _httpManger = HttpManagerFile._internal();

  factory HttpManagerFile() {
    return _httpManger;
  }

  ///配置dio
  void initDio() {
    _dio = Dio();

    _dio.interceptors.add(new LogsInterceptors());
    _dio.interceptors.add(InterceptorsWrapper(
      onError: (DioError e) {
        trueDebugPrint('flutterhttp Dio error with request: ${e.request.uri}');
        trueDebugPrint('flutterhttp Request data: ${e.request.data}');
        trueDebugPrint('flutterhttp Dio error: ${e.message}');
        return e;
      },
    ));


    _dio.options.connectTimeout = 15 * 1000; //5s

//代理设置
//     if (Config.DEBUG) {
// //      _dio.interceptors
// //          .add(WhLogInterceptor(requestBody: true, responseBody: true));
//       //此处可以增加配置项，留一个设置代理的用户给测试人员配置，然后动态读取
//
// //      print("--prox----${proxy}----${SpUtils.getBool(SpConstanst.httpProxyEnable)}=====-");
//       if(proxy!=null&&proxy!=""&&SpUtils.getBool("")) {
//         (_dio.httpClientAdapter as DefaultHttpClientAdapter)
//             .onHttpClientCreate =
//             (client) {
//           // config the http client
//           client.findProxy = (uri) {
//             //proxy all request to localhost:8888
//             return "PROXY " + SpUtils.getString("");
//           };
//
//
//           client.badCertificateCallback =
//               (X509Certificate cert, String host, int port) => true;
//           // you can also create a new HttpClient to dio
//           // return new HttpClient();
//         };
//       }
//     } else {
//       //证书配置，忽略证书
//       String PEM = "XXXXX"; // certificate content
//       (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
//           (client) {
//         client.badCertificateCallback =
//             (X509Certificate cert, String host, int port) {
//           return true; //返回 true 表明 忽略 证书校验
//         };
//       };
//     }
  }

}

